;(async function () {
  const express = require("express")
  const db = require("./db/db")
  const model = require("./db/model")
  try {
    await db
    console.log("数据库创建成功")
    const app = express()
    app.use(express.urlencoded({ extended: false }))
    app.get("/find", async (req, res) => {
      const result = await model.find()
      res.send(result)
    })
    app.post("/addUser", async (req, res) => {
      const { name, age } = req.body
      try {
        await model.create({ name, age })
        res.send("添加成功")
      } catch (err) {
        res.send("添加失败")
      }
    })
    app.listen(5000, (err) => {
      if (err) console.log("失败")
      else console.log("成功")
    })
  } catch (err) {
    console.log("数据库创建失败", err)
  }
})()
